<html>
<head>
    <meta charset="UTF-8">
    <title>cookie test</title>
    <style>
        a{
            display: block;
            font-size: 30px;
        }
    </style>
</head>

<body>
<a href="#" onclick=checkCookie()>检查有没有cookie</a>
<a href="#" onclick=clearAllCookie()>清除所有cookie</a>
<a href="#" onclick=view_cookie()>查看cookie</a>
<a href="#" onclick=view_cookie2()>查看cookie__2</a>
<a href="#" onclick=rename_cookie()>修改cookie</a>
<a href="#" onclick=add_cookie()>Add__cookie</a>
</body>



<script type="text/javascript">


    setCookie('username', "初始化", 365);
    setCookie('password', "password", 365);


    // JavaScript 遍历所有cookie
    var aCookie = document.cookie.split(";");
    var re = '';
    for (var i = 0; i < aCookie.length; i++) {
        var aCrumb = aCookie[i].split("=");
        if (aCrumb[0].toString().trim() === 'order_list') {
            continue;
        }
        re += (aCrumb[0] + " = " + unescape(aCrumb[1]) + '\n');    /*   unescape  处理中文乱码问题             */
    }
    alert('cookie info:\n' + re);






    //清除所有cookie函数
    function clearAllCookie() {
        var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
        if(keys) {
            for(var i = keys.length; i--;)
                document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
            alert('cookie is clear!!!!!')
        }
    }

    //拿到cookie
    function getCookie(c_name) {
        if(document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if(c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if(c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end))
            }
        }
        return ""
    }

    // add_cookie
    function add_cookie() {
        username = prompt('Please enter your name:', "");
        if(username != null && username != "") {
            setCookie('username', username, 365)
        }
    }

    //设置cookie
    function setCookie(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) +  ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
    }


    //检查cookie
    function checkCookie() {
        username = getCookie('username')
        if(username != null && username != "") {
            alert('Welcome again ' + username + '!')
        } else {
            alert("cokkie is null.....")
        }
    }

    //查看cookie
    function view_cookie() {
        username = getCookie('username');
        alert('username = ' + username)
    }

    //查看cookie2
    function view_cookie2() {
        password = getCookie('password');
        alert('password = ' + password)
    }

    //重命名cookie
    function rename_cookie() {
        username = prompt('enter your new name:', "");
        if(username != null && username != "") {
            setCookie('username', username, 365)
        }
    }

</script>
</html>

